package com.yili.oa.dao.extend;

import com.yili.oa.entity.FtBetRecordSearch;
import com.yili.oa.utils.ListUtils;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public class FtBetRecordExtendSqlProvider {
    public String get(FtBetRecordSearch searchBase){
        StringBuffer sb = new StringBuffer();
        String sql = "SELECT a.id,FROM_UNIXTIME(create_time_utc/1000) as betTime,FROM_UNIXTIME(payout_time_utc/1000) as pieTime,a.bet_no,a.bet_amount,a.pie_awarded,a.game_kind,a.game_platform,a.game_id,a.user_id,b.name as game_name from (select * from ft_bet_record where 1=1";
        sb.append(sql);
        if(ListUtils.isNotNullOrEmpty(searchBase.getIds())){
            List<Long> ids = searchBase.getIds();
            sb.append(" and user_id in (");
            for (int i = 0; i < ids.size(); i++) {
                if(i!=ids.size()-1){
                    sb.append(ids.get(i)+",");
                }else{
                    sb.append(ids.get(i)+")");
                }
            }

        }
        if(searchBase.getGameKind()!=null){
            sb.append(" and game_kind =" + searchBase.getGameKind());
        }else{
            sb.append(" and game_kind = 1");
        }
        if(searchBase.getGamePlatform()!=null){
            sb.append(" and game_platform =" + searchBase.getGamePlatform());
        }
        if(searchBase.getBegin()!=null){
            sb.append(" and payout_time_utc >=" + searchBase.getBegin());
        }
        if (searchBase.getEnd()!= null) {
            sb.append(" and payout_time_utc <=" + searchBase.getEnd());
        }
        sb.append(" )as a left join inventory_control as b on a.game_id = b.index ");
        if(searchBase.getOrderBy()!=null){
            sb.append(" ORDER BY "+searchBase.getOrderBy());
        }else{
            sb.append(" ORDER BY create_time_utc desc ");
        }
        return sb.toString();


    }

    public String getAccountStatistics(FtBetRecordSearch searchBase){
        StringBuffer sb = new StringBuffer();
        String sql = "select sum(bet_amount) as accumulateBet,sum(pie_awarded) as pieAwarded\n" +
                " from ft_bet_record where user_id = "+searchBase.getUserId() +" and game_kind = "+searchBase.getGameKind();
        sb.append(sql);
        sb.append(" and create_time_utc >=" + searchBase.getBegin());
        sb.append(" and create_time_utc <=" + searchBase.getEnd());
        return sb.toString();
    }

    public String getRewardRecord(@Param("userId")Long userId,@Param("gameKind")Long gameKind,@Param("datetime")Date datetime){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT SUM(BET_AMOUNT) AS BETSUM,game_platform FROM ft_bet_record ");
        sb.append("WHERE (user_id =#{userId,jdbcType=BIGINT} AND " +
                "game_kind =#{gameKind,jdbcType=BIGINT} AND ");
        if (datetime==null){
            sb.append("xima_time is null)");
        }else {
            sb.append("xima_time =#{datetime,jdbcType=DATETIME})");
        }
        sb.append(" GROUP BY game_platform");
        return sb.toString();
    }

    public String getRewardRecord2(@Param("userId")Long userId,@Param("gameKind")Long gameKind,@Param("datetime")Date datetime){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT SUM(BET_AMOUNT) AS BETSUM,game_id FROM ft_bet_record ");
        sb.append("WHERE (user_id =#{userId,jdbcType=BIGINT} AND " +
                "game_kind =#{gameKind,jdbcType=BIGINT} AND ");
        if (datetime==null){
            sb.append("xima_time is null)");
        }else {
            sb.append("xima_time =#{datetime,jdbcType=DATETIME})");
        }
        sb.append(" GROUP BY game_id");
        return sb.toString();
    }


    public String getSumBet(@Param("userId")Long userId){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT SUM(BET_AMOUNT) AS BETSUM FROM ft_bet_record ");
        sb.append("WHERE (user_id =#{userId,jdbcType=BIGINT} AND " +
                "xima_time is null) ");
        return sb.toString();
    }


}
